//属性拷贝
export function proCopy(obj, attrs) {
    /*
        @param  
            1. obj 对象
            2. attrs 需要转换的属性
     */
    var result = [],
        i,
        iLen,
        j,
        jLen,
        pro,
        exPro,
        items,
        recursion = false,
        argArr = [];
    //如果参数大于3个
    if(arguments.length > 2) {
        recursion = true;
        argArr = Array.prototype.slice.call(arguments, 0);
        obj = argArr.slice(0, -1);
        attrs = argArr[argArr.length - 1];
    }
    //递归
    if(recursion) {
        for(i = 0, iLen = obj.length; i < iLen; i++) {
            for(j = 0, jLen = attrs.length; j < jLen; j++) {
                //获取属性
                pro = exPro = attrs[j];
                if(attrs[j].indexOf('-') != -1) {
                    items = attrs[j].split('-');
                    pro = items[0];
                    exPro = items[1];
                }
                if(obj[i].hasOwnProperty(pro)) {
                    result.push(encodeURIComponent(exPro) + '=' + encodeURIComponent(obj[i][pro]));
                }
            }
        }
    } else {
        for(i = 0, iLen = attrs.length; i < iLen; i++) {
            //获取属性
            pro = exPro = attrs[i];
            if(attrs[i].indexOf('-') != -1) {
                items = attrs[i].split('-');
                pro = items[0];
                exPro = items[1];
            }
            if(obj.hasOwnProperty(pro)) {
                result.push(encodeURIComponent(exPro) + '=' + encodeURIComponent(obj[pro]));
            }
        }
    }
    return result.join('&');
}

//百分比计算
export function percent(dividend,divisor){
    //如果被除数为 0，直接返回 0%
    if(divisor == 0){
        return '0%';
    }else{
        return parseInt((dividend / divisor)*100)+'%';
    }
}

//对象转换为查询字符串
export function objToString(obj, attrs) {
    /*
        @param  
            1. obj 对象
            2. attrs 需要转换的属性
     */
    var result = [],
        i,
        iLen,
        j,
        jLen,
        pro,
        exPro,
        items,
        recursion = false,
        argArr = [];
    //如果参数大于3个
    if(arguments.length > 2) {
        recursion = true;
        argArr = Array.prototype.slice.call(arguments, 0);
        obj = argArr.slice(0, -1);
        attrs = argArr[argArr.length - 1];
    }
    //递归
    if(recursion) {
        for(i = 0, iLen = obj.length; i < iLen; i++) {
            for(j = 0, jLen = attrs.length; j < jLen; j++) {
                //获取属性
                pro = exPro = attrs[j];
                if(attrs[j].indexOf('-') != -1) {
                    items = attrs[j].split('-');
                    pro = items[0];
                    exPro = items[1];
                }
                if(pro in obj[i]) {
                    result.push(encodeURIComponent(exPro) + '=' + encodeURIComponent(obj[i][pro]));
                }
            }
        }
    } else {
        for(i = 0, iLen = attrs.length; i < iLen; i++) {
            //获取属性
            pro = exPro = attrs[i];
            if(attrs[i].indexOf('-') != -1) {
                items = attrs[i].split('-');
                pro = items[0];
                exPro = items[1];
            }
            if(pro in obj) {
                result.push(encodeURIComponent(exPro) + '=' + encodeURIComponent(obj[pro]));
            }
        }
    }
    return result.join('&');
}